package com.seafood.config;

import com.seafood.websocket.WebSocketHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * WebSocket配置类
 * 
 * @author System
 * @since 2024-01-01
 */
@Slf4j
@Configuration
@EnableWebSocket
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketConfigurer {

    private final WebSocketHandler webSocketHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        log.info("注册WebSocket处理器");
        
        // 注册WebSocket处理器
        registry.addHandler(webSocketHandler, "/websocket")
                .setAllowedOrigins("*") // 允许跨域
                .withSockJS(); // 启用SockJS支持
        
        log.info("WebSocket处理器注册完成");
    }
}